/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.letmeshare.stockkeeper.stock.service;

import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;
import org.letmeshare.stockkeeper.PersistenceManager;
import org.letmeshare.stockkeeper.stock.entities.Category;
import org.letmeshare.stockkeeper.stock.entities.Item;
import org.letmeshare.stockkeeper.stock.entities.Stock;
import org.letmeshare.stockkeeper.stock.module.CategoryModule;
import org.letmeshare.stockkeeper.stock.module.ItemModule;
import org.letmeshare.stockkeeper.stock.module.StockModule;

/**
 *
 * @author ibisuser1
 */
public class StockService {
    private EntityManager em;
    private StockModule stockModule;
    private ItemModule itemModule;
    private EntityTransaction txn;
    
    public static StockService getInstance() {
        return new StockService();
    }
    
    private StockService() {
        em = PersistenceManager.getEntityManager();
        stockModule = StockModule.getInstance(em);
        itemModule = ItemModule.getInstance(em);
    }
    
    public List<Stock> getAllStocks() {
       EntityManager manager= PersistenceManager.getEntityManager();
       return StockModule.getInstance(manager).getAllStocks();
    }
    
    public List<Stock> lookup(String queryString) {
        return stockModule.lookUpStocksByItemFeatures(queryString);
    }
    
    public void createItem(Item item) {
        if (txn!=null && txn.isActive()) {
            txn.rollback();
        }
        txn = em.getTransaction();
        txn.begin();
        itemModule.createItem(item);
        stockModule.createStock(item);
        txn.commit();
    }
    
    public void createCategory(Category category) {
        CategoryModule categoryModule = CategoryModule.getInstance(em);
        if (txn!=null && txn.isActive()) {
            txn.rollback();
        }
        txn = em.getTransaction();
        txn.begin();
        categoryModule.createCategory(category);
        txn.commit();
    }
    
    public void deleteCategory(Category category) {
        CategoryModule categoryModule = CategoryModule.getInstance(em);
        if (txn!=null && txn.isActive()) {
            txn.rollback();
        }
        txn = em.getTransaction();
        txn.begin();
        categoryModule.deleteCategory(category);
        txn.commit();
    }
    
    public void deleteItem(Item item) {
        if (txn!=null && txn.isActive()) {
            txn.rollback();
        }
        txn = em.getTransaction();
        txn.begin();
        itemModule.deleteItem(item);
        txn.commit();
    }
    
    public void updateItem(Item item) {
        if (txn!=null && txn.isActive()) {
            txn.rollback();
        }
        txn = em.getTransaction();
        txn.begin();
        itemModule.updateItem(item);
        txn.commit();
    }
    
    public void updateCategory(Category category) {
        CategoryModule categoryModule = CategoryModule.getInstance(em);
        if (txn!=null && txn.isActive()) {
            txn.rollback();
        }
        txn = em.getTransaction();
        txn.begin();
        categoryModule.updateCategory(category);
        txn.commit();
    }
    public Stock getStock(String id){
        return em.find(Stock.class, id);
    }
    public List<Category> getAllCategorys(){
        return null;//TODO
    }
}
